package com.fc.advbox.controller.api;

import com.fc.advbox.common.ResponseJson;
import com.fc.advbox.controller.admin.BaseController;
import com.fc.advbox.entity.ContractEntity;
import com.fc.advbox.entity.PubInfoEntity;
import com.fc.advbox.service.impl.ContractServiceImpl;
import com.fc.advbox.service.impl.OrgServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Description:
 * User: Charles.Gao
 * Date: 2018-07-14 17:00
 */
@RestController
@RequestMapping("/api/remind")
@Api(value="提醒API", tags={"提醒接口"})
public class RemindController extends BaseController {

    @Autowired
    private OrgServiceImpl orgService;

    @Autowired
    private ContractServiceImpl contractService;

    @ApiOperation(value = "GET -- 获取提醒列表",
            notes = " REST 方式请求。"+
                    "Request Parameter: <ul>" +
                    "<li><i>可选参数: orgId: , 如不输入，默认为国鑫智能</i></li>" +
                    "<li><i>可选参数: limit: 每页记录数</i></li>" +
                    "<li><i>可选参数: offset: 第几页</i></li>" +
                    "</ul>",
            response = ResponseJson.class,
            httpMethod = "GET",
            consumes = "application/json",
            produces = "application/json",
            code = 200
    )
    @GetMapping("/list")
    public ResponseJson doQueryList(@RequestParam(name="orgId", required=false) String orgId,
                                    @RequestParam(name="limit", required=false) Integer limit,
                                    @RequestParam(name="page", required=false) Integer page) {
        Map<String, Object> paramMap = new HashedMap();
        Long defaultOrgId = orgService.getDefaultOrg().getId();
        if(orgId ==null || "".equals(orgId) || orgId.equals(defaultOrgId.toString())){
            paramMap.put("jiaOrgId",defaultOrgId);
        }else{
            paramMap.put("yiOrgId",orgId);
        }
        paramMap.put("limit",limit);
        paramMap.put("page",page);
        paramMap.put("interva",30);//TODO:change 30 to config table

        //校对并赋值 offset和limit
        calculatePagination(paramMap);

        List<ContractEntity> infoList = contractService.queryList(paramMap);
        for(ContractEntity c: infoList){
            if(c.getInterva()< 0 && !"termination".equals(c.getStatus())){
                c.setStatus("已过期"+Math.abs(c.getInterva())+"天");
            }else{
                c.setStatus("即将"+Math.abs(c.getInterva())+"天后过期");
            }
        }
        return ResponseJson.success(infoList);
    }
}
